package StudyCode.UI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

/**
 * @author lixiaoyao
 * @date 2018/11/13 0013、下午 5:16
 * @description 鼠标示例代码
 */
public class MouseFrame extends JFrame {
	public MouseFrame() {
		add(new MouseComponent());
		pack();
	}
}


class MouseComponent extends JComponent {

	private static final int DEFAULT_WIDTH = 300;
	private static final int DEFAULT_HEIGHT = 200;

	private static final int SIDE_LENGTH = 100;
	private ArrayList<Rectangle2D> squares;
	private Rectangle2D current;

	public MouseComponent() {
		this.squares = new ArrayList<>();
		this.current = null;

		addMouseListener(new MouseHandler());
		addMouseMotionListener(new MouseMotionHandler());
	}

	public Dimension getPreferredSize() {
		return new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	}

	@Override
	protected void paintComponent(Graphics graphics) {
		Graphics2D graphics2D = (Graphics2D) graphics;

		// draw all squares
		for (Rectangle2D rectangle2D : this.squares) {
			graphics2D.draw(rectangle2D);
		}
	}

	public Rectangle2D find(Point2D point2D) {
		for (Rectangle2D rectangle2D : this.squares) {
			if (rectangle2D.contains(point2D)) {
				return rectangle2D;
			}
		}
		return null;
	}

	public void add(Point2D point2D) {
		this.current = new Rectangle2D.Double(point2D.getX() - SIDE_LENGTH / 2, point2D.getY() - SIDE_LENGTH / 2, SIDE_LENGTH, SIDE_LENGTH);
		this.squares.add(this.current);
		repaint();
	}

	public void remove(Rectangle2D rectangle2D) {
		if (rectangle2D == null) {
			return;
		}
		if (rectangle2D == this.current) {
			this.current = null;
		}
		this.squares.remove(rectangle2D);
		repaint();
	}

	private class MouseHandler extends MouseAdapter {

		@Override
		public void mousePressed(MouseEvent e) {
			// add a new square if the cursor isn't inside a square
			current = find(e.getPoint());
			if (current == null) {
				add(e.getPoint());
			}
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			// remove the current squares if double clicked
			current = find(e.getPoint());
			if (current != null && e.getClickCount() >= 2) {
				remove(current);
			}
		}
	}

	private class MouseMotionHandler implements MouseMotionListener {

		@Override
		public void mouseDragged(MouseEvent e) {
			if (current != null) {
				// drag the current rectangle to center it at (x,y)
				current.setFrame(e.getX() - SIDE_LENGTH / 2, e.getY() - SIDE_LENGTH / 2, SIDE_LENGTH, SIDE_LENGTH);
				repaint();
			}
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			// set the mouse cursor to cross hairs if it is inside
			// a rectangle

			if (find(e.getPoint()) == null) {
				setCursor(Cursor.getDefaultCursor());
			} else {
				setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
			}
		}
	}

	public static void main(String[] args) {
		JFrame frame = new MouseFrame();
		frame.setSize(800, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

}